package com.wzh.test;

/**
 * 双向链表增删改查
 */
public class TwoWayLinkedListDemo {
    public static void main(String[] args) {
        TwoWayLinkedList list = new TwoWayLinkedList();
        list.add(new TwoWayNode(1,"宋江"));

        list.add(new TwoWayNode(3,"吴用"));
        list.add(new TwoWayNode(2,"卢俊义"));

        list.modify(new TwoWayNode(2,"小卢"));

        list.del(3);
        list.print();

    }
}

class TwoWayLinkedList{
    private TwoWayNode first;
    private TwoWayNode last;

    public TwoWayLinkedList(){

    }

    public void add(TwoWayNode node){
        if(this.first == null){
            //添加第一个节点
            first = node;
            last = node;
            return;
        }
        TwoWayNode currentNode = first;
        while (true){
            if(currentNode.getNo() > node.getNo()){
                //插入节点
                currentNode.getPrev().setNext(node);
                node.setPrev(currentNode.getPrev());
                node.setNext(currentNode);
                currentNode.setPrev(node);
                break;
            }
            if(currentNode == last){
                currentNode.setNext(node);
                node.setPrev(currentNode);
                last = node;
                break;
            }
            currentNode = currentNode.getNext();
        }
    }

    public void modify(TwoWayNode node){
        TwoWayNode currentNode = first;
        while (currentNode!=null){
            if(currentNode.getNo() == node.getNo()){
                currentNode.setName(node.getName());
                break;
            }
            currentNode = currentNode.getNext();
        }
    }

    public void del(int no){
        TwoWayNode currentNode = first;
        while (currentNode != null){
            if(currentNode.getNo() == no){
                if(currentNode == first){
                    currentNode.getNext().setPrev(null);
                    first = currentNode.getNext();
                    break;
                }
                if(currentNode == last){
                    currentNode.getPrev().setNext(null);
                    last = currentNode.getPrev();
                    break;
                }
                currentNode.getPrev().setNext(currentNode.getNext());
                currentNode.getNext().setPrev(currentNode.getPrev());
                currentNode = null;
                break;
            }
            currentNode = currentNode.getNext();
        }
    }
    public void print(){
        TwoWayNode currentNode = first;
        while (currentNode != null){
            System.out.println(currentNode);
            currentNode = currentNode.getNext();
        }
    }

}
class TwoWayNode{
    private int no;
    private String name;
    private TwoWayNode next;
    private TwoWayNode prev;

    public TwoWayNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public TwoWayNode getNext() {
        return next;
    }

    public void setNext(TwoWayNode next) {
        this.next = next;
    }

    public TwoWayNode getPrev() {
        return prev;
    }

    public void setPrev(TwoWayNode prev) {
        this.prev = prev;
    }

    @Override
    public String toString() {
        return "TwoWayNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}
